package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//@Controller
@RestController //使用此注解代替@Controller和@ResponseBody注解
@RequestMapping("/checkItem")
public class CheckItemController {

    @Reference
    private CheckItemService checkItemService;

    //添加检查项
    @RequestMapping("/add")
    //@ResponseBody
    @PreAuthorize("hasAuthority('CHECKITEM_ADD')")
    public Result addCheckItem(@RequestBody CheckItem checkItem){

        try {
            //此时调用service方法
            checkItemService.addCheckItem(checkItem);
            return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
        }catch (Exception e){
            //返回结果对象
         return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
    }

    //查询所有检查项，做添加检查组时的显示，选择
    @RequestMapping("/findAll")
    //@ResponseBody
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    public Result findAll(){
        try {
            //此时调用service方法
            List<CheckItem> list = checkItemService.findAll();
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,list );
        }catch (Exception e){
            //返回结果对象
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,null);
        }
    }

    //按条件分页查询检查项
    @RequestMapping("/findByCondition")
    //@ResponseBody
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    public PageResult findByCondition(@RequestBody QueryPageBean queryPageBean){

        try {
            //此时调用service方法
            PageResult pageResult = checkItemService.findByCondition(queryPageBean);
            return pageResult;
        }catch (Exception e){
            //返回结果对象
            return null;
        }
    }

    //删除检查项
    @RequestMapping("/del/{id}")
    //@ResponseBody
    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")
    public Result delCheckItem(@PathVariable Integer id){

        System.out.println("删除检查项方法执行了..."+id);
        try {
            //此时调用service方法
            checkItemService.delCheckItem(id);
            return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        }catch (Exception e){
            //返回结果对象
            return new Result(false, MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
    }

    //更新检查项
    @RequestMapping("/update")
    //@ResponseBody
    @PreAuthorize("hasAuthority('CHECKITEM_EDIT')")
    public Result updateCheckItem(@RequestBody CheckItem checkItem){

        try {
            //此时调用service方法
            checkItemService.updateCheckItem(checkItem);
            return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
        }catch (Exception e){
            //返回结果对象
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }
    }
}
